package cn.hema.controller;

import cn.hema.Client.BookServiceClient;
import cn.hema.model.Book;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@AllArgsConstructor
@RestController
@Slf4j
public class BookServiceClientController {
    @Qualifier("cn.hema.Client.BookServiceClient")
    @Autowired
    private final BookServiceClient bookServiceClient;

    @GetMapping("/{id}")
    public Book getBookById(@PathVariable Long id) {
        return bookServiceClient.findById(id);
    }
    @PostMapping
    public ResponseEntity<Book> addBook(@RequestBody Book book) {
        bookServiceClient.addBook(book);
        System.out.println("in client : " + book);
        return ResponseEntity.ok(book);
    }

    // 删除书籍
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
        bookServiceClient.deleteBook(id);
        return ResponseEntity.noContent().build();
    }

    // 更新书籍信息
    @PutMapping("/{id}")
    public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book book) {
        book.setId(id); // 确保ID一致性
        bookServiceClient.updateBook(id, book);
        return ResponseEntity.ok(book);
    }

    // 查询所有书籍
    @GetMapping
    public ResponseEntity<List<Book>> getAllBooks() {
        return ResponseEntity.ok(bookServiceClient.getAllBooks());
    }
}